<?php
/**
 * Created by PhpStorm.
 * User: itboye
 * Date: 2018/5/21
 * Time: 13:35
 */



$repo = file_get_contents("php://input");
$repo = json_decode($repo, JSON_OBJECT_AS_ARRAY);
if (array_key_exists('repository', $repo)) {
    $apiToken = $_GET['api_token'];
    $userName = $_GET['username'];
    $packagistUrl = "https://packagist.org/api/update-package?username=".$userName."&apiToken=".$apiToken;
    $repository = $repo['repository'];
    $url = $repository['clone_url'];
    $ch = curl_init();
    $data = ['repository'=>['url'=>$url]];
    $data  = json_encode($data);
    $headerArray = array("Content-type:application/json");
    curl_setopt($ch, CURLOPT_URL, $packagistUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT,1);//启用时追踪句柄的请求字符串。
    $ret = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    echo $ret;
} else {
    echo "地址后面需要拼接api_token,username 参数，参考 https://packagist.org/about#how-to-update-packages ";
}